<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/5/15 17:14
 * @copyright PanxSoft Inc.
 */

namespace App\Services\DBHelpers;


use App\Models\Customer\Customer;
use App\Models\Store\Store;
use App\Services\BaiduGeoService\MapApi;

class StoreAddressToLocation
{
    /**
     * App\Services\DBHelpers\StoreAddressToLocation::updateCustomerPanxStoresLocation()
     */
    public static function updateCustomerPanxStoresLocation()
    {
        $stores = (new Customer)->find(1)->stores;

        $geo_service = new MapApi();

        $stores->each(function (Store $store) use ($geo_service){

            $res = $geo_service->getLocationQuery($store->province . $store->city . $store->district . $store->address);
            if ($res['status'] === MapApi::RESPONSE_STATUS_OK) {
                $store->update([
                    'longitude' => $res['result']['location']['lng'],
                    'latitude' => $res['result']['location']['lat'],
                ]);
            } else {
                echo $store->address . ' not found!' . PHP_EOL;
            }
        });
    }
}